//
//  PictureShowViewController.m
//  YBCustomCamera
//
//  Created by 爱尚 on 2018/9/18.
//  Copyright © 2018年 中天易观. All rights reserved.
//

#import "PictureShowViewController.h"

#define KScreenWidth  [UIScreen mainScreen].bounds.size.width
#define KScreenHeight  [UIScreen mainScreen].bounds.size.height

@interface PictureShowViewController ()
@property(nonatomic,strong)CAShapeLayer *shapeLayer;
@property(nonatomic,strong)UIView *sweepView;

@end

@implementation PictureShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
    
}

-(void)useBtnAction{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:UserHeaderIconNotification object:nil userInfo:[NSDictionary dictionaryWithObject:self.pic_image forKey:@"pic_image"]];
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}


/**
 *从图片中按指定的位置大小截取图片的一部分
 * UIImage image 原始的图片
 * CGRect rect 要截取的区域
 */
+(UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect{
    
    //将UIImage转换成CGImageRef
    CGImageRef sourceImageRef = [image CGImage];
    
    //按照给定的矩形区域进行剪裁
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    
    //将CGImageRef转换成UIImage
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    
    //返回剪裁后的图片
    return newImage;
}


-(void)initViews{
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenHeight - 100, KScreenWidth, 100)];
    bottomView.backgroundColor = [UIColor colorWithRed:41/255.0 green:43/255.0 blue:45/255.0 alpha:1.0];
    UIButton *repayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [repayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [repayBtn setTitle:@"重拍" forState:UIControlStateNormal];
    repayBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    repayBtn.frame = CGRectMake(0, 0, 100,100);
    [repayBtn addTarget:self action:@selector(repayBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    [bottomView addSubview:repayBtn];
    
    
    UIButton *useBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [useBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [useBtn setTitle:@"使用照片" forState:UIControlStateNormal];
    useBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    useBtn.frame = CGRectMake(KScreenWidth-100, 0, 100, 100);
    [useBtn addTarget:self action:@selector(useBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:useBtn];
    
    
    UIImageView *picImageView = [[UIImageView alloc]init];
    picImageView.image = self.pic_image;
    picImageView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    [self.view addSubview:picImageView];
    
    
    [self.view.layer addSublayer:self.shapeLayer];
    [self.view addSubview:bottomView];
    
    
    
    
}

-(CAShapeLayer *)shapeLayer{
    if (!_shapeLayer) {
        _shapeLayer = [CAShapeLayer layer];
        _shapeLayer.path = [UIBezierPath bezierPathWithRect:[UIScreen mainScreen].bounds].CGPath;
        _shapeLayer.fillColor = [UIColor blackColor].CGColor;
        _shapeLayer.opacity = 0.7;
        _shapeLayer.mask = [self generateMaskLayerWithRect:[UIScreen mainScreen].bounds exceptRect:self.sweepView.frame];
    }
    return _shapeLayer;
}

-(UIView *)sweepView{
    if (!_sweepView) {
        _sweepView = [[UIView alloc]init];
        _sweepView.frame = CGRectMake(KScreenWidth/6, KScreenWidth/4+60, KScreenWidth/3*2, KScreenWidth/3*2);
        _sweepView.clipsToBounds = YES;
        _sweepView.layer.borderColor = [UIColor whiteColor].CGColor;
        _sweepView.layer.borderWidth = 0.5;
        [self.view addSubview:_sweepView];
    }
    return _sweepView;
}

- (CAShapeLayer *)generateMaskLayerWithRect: (CGRect)rect exceptRect: (CGRect)exceptRect {
    CAShapeLayer * maskLayer = [CAShapeLayer layer];
    if (!CGRectContainsRect(rect, exceptRect)) {
        return nil;
    }
    else if (CGRectEqualToRect(exceptRect, CGRectZero)) {
        maskLayer.path = [UIBezierPath bezierPathWithRect: rect].CGPath;
        return maskLayer;
    }
    CGFloat boundsInitX = CGRectGetMinX(rect);
    CGFloat boundsInitY = CGRectGetMinY(rect);
    CGFloat boundsWidth = CGRectGetWidth(rect);
    CGFloat boundsHeight = CGRectGetHeight(rect);
    CGFloat minX = CGRectGetMinX(exceptRect);
    CGFloat maxX = CGRectGetMaxX(exceptRect);
    CGFloat minY = CGRectGetMinY(exceptRect);
    CGFloat maxY = CGRectGetMaxY(exceptRect);
    CGFloat width = CGRectGetWidth(exceptRect);
    UIBezierPath * path = [UIBezierPath bezierPathWithRect: CGRectMake(boundsInitX, boundsInitY, minX, boundsHeight)];
    [path appendPath: [UIBezierPath bezierPathWithRect: CGRectMake(minX, boundsInitY, width, minY)]];
    [path appendPath: [UIBezierPath bezierPathWithRect: CGRectMake(maxX, boundsInitY, boundsWidth - maxX, boundsHeight)]];
    [path appendPath: [UIBezierPath bezierPathWithRect: CGRectMake(minX, maxY, width, boundsHeight - maxY)]];
    maskLayer.path = path.CGPath;
    return maskLayer;
}
-(void)repayBtnAction{
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
